import { ref,  onMounted,h, getCurrentInstance,AppContext,toRaw} from "vue";
import { layer } from '@layui/layui-vue';
    
class MyWin {
    public static appContext:any=null;
     
    private static winArr=Array<string>() ;
     
    
    static   open(opt:any,appContext:any):String {
        let openopt={
            title: '',
            content: "",
            shade: true,
            shadeOpacity	:0.1,
            shadeClose:false,
            closeBtn:"1",
            isOutAnim	:true,
            appContext:undefined
        }
        const dstobj = Object.assign( {}, openopt, opt); 
           if(appContext){ 
            dstobj.appContext=appContext;
           }
            

            if(!dstobj.area){
                let size=MyWin.getViewportSize();
                if(dstobj.w&&dstobj.h){
                    if(dstobj.w>size.width){
                        dstobj.w=size.width-40;
                    }
                    if(dstobj.h>size.height){
                        dstobj.h=size.height-40;
                    }
                    dstobj.area=[dstobj.w+"px",dstobj.h+"px"];
                }
            }
         
        dstobj.end=(  )=>{ 
            var id=MyWin.winArr.pop(); 
        }
        var id=layer.open( dstobj );
        MyWin.winArr.push(id  ); 
        return id;
    };
    static   close():void{
      
        if(MyWin.winArr.length>0){
            var  id=MyWin.winArr[MyWin.winArr.length-1];
            layer.close(id);
        } 
    };
    private static alertId="";
    static alert(msg:any,opt:any){
        const config={
            title:"系统提示",
            shade:true,   
            onOK:function(){}     ,
            btn:[
                {text:'确定', callback: function(id:any){layer.close(id);
                    if(opt.onOK){
                        opt.onOK();
                    }
                    layer.close(MyWin.alertId);
                    MyWin.alertId=""; 
                }},
                
            ]
        }
         

        const dstobj = Object.assign({},config, opt);

        
        if(MyWin.alertId!=""){
            layer.close(MyWin.alertId); 
        }
        MyWin.alertId=layer.confirm(msg,   dstobj);
    }
    static confirm(msg:any,opt:any){
        //{title:"",msg:"",onOK,onCancel}
        if(!opt.title){
            opt.title="系统提示";
        }
        
        layer.confirm(msg, 
            {   
                title:opt.title,
                btn:
                [
                    {text:'确定', callback: function(id:any){layer.close(id);
                        if(opt.onOK){
                            opt.onOK();
                        }
                    }},
                    {
                        text:'取消', callback: function(id:any){layer.close(id);
                            if(opt.onCancel){
                                opt.onCancel();
                            }
                    
                    }}
                ]
            })
    }

    static getViewportSize(){
        return {
          width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
          height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
        };
    }

    public appContext:any=undefined;
    public constructor(){
        let d=getCurrentInstance();
       
        if(d){

             this.appContext=d.appContext;
              
        }   
    }
    public open(opt:any):String{  
        return MyWin.open(opt, toRaw(this.appContext) );
    }
    public close(id:string){
        if(id){
            layer.close(id);
        }else{
            MyWin.close();
        } 
    }
     
    
  
}
export default MyWin